package com.dynamic.mybatis.core.enums;

import org.apache.ibatis.mapping.SqlCommandType;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface CommandType {

  String name();
  String commandType();

  List<CommandType> executableCommandType = Stream.of(StandardCommand.INSERT,StandardCommand.UPDATE,StandardCommand.DELETE,StandardCommand.SELECT,StandardCommand.SAVE).collect(Collectors.toList());

   enum  StandardCommand implements CommandType{
      UNKNOWN,
      INSERT,
      UPDATE,
      DELETE,
      SELECT,
      SAVE,
      FLUSH,
      MERGE,//多个command合并，参数和结果集将会合并
      ;
      public String commandType(){
         return this.name();
      }
  }

  static CommandType getCommandType(String commandType ){
      for(StandardCommand standardCommand : StandardCommand.values()){
          if(standardCommand.name().equalsIgnoreCase(commandType)){
              return standardCommand;
          }
      }
      return null;
  }

  static SqlCommandType getSqlCommandType(CommandType commandType){
        if(commandType == null){
            return null;
        }
        for(SqlCommandType sqlCommandType : SqlCommandType.values()){
            if(commandType.commandType().equals(sqlCommandType.name())){
                return sqlCommandType;
            }
        }
        if(StandardCommand.SAVE == commandType){
            return SqlCommandType.UPDATE;
        }
        return null;
    }
}
